package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "购物车相关接口")
@RequestMapping("/user/shoppingCart")
@RestController
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCartDTO 购物车数据
     */
    @ApiOperation(value = "添加购物车")
    @PostMapping("/add")
    public Result<Object> addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车：{}", shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 删减购物车
     * @param shoppingCartDTO 购物车数据
     */
    @ApiOperation(value = "删减购物车")
    @PostMapping("/sub")
    public Result<Object> subShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删减购物车：{}", shoppingCartDTO);
        shoppingCartService.subShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查询购物车列表
     * @return 购物车列表
     */
    @ApiOperation(value = "查询购物车列表")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> getShoppingCartList() {
        log.info("查询购物车列表");
        List<ShoppingCart> shoppingCartList = shoppingCartService.getShoppingCartList();
        return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     */
    @ApiOperation(value = "清空购物车")
    @DeleteMapping("/clean")
    public Result<Object> cleanShoppingCart() {
        log.info("清空购物车");
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }
}
